package com.ruoyi.common.utils.login;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class WebServiceMdms {
	//httpKey
	private final static String HTTP_KEY="B5618B7247257862E050F40A65E7F9C6";
	// 读取超时
	private final static int SOCKET_TIMEOUT = 10000;
	// 连接超时
	private final static int CONNECTION_TIMEOUT = 10000;
	// 每个HOST的最大连接数量
	private final static int MAX_CONN_PRE_HOST = 20;
	// 连接池的最大连接数
	private final static int MAX_CONN = 30;
	// 连接池
	private final static HttpConnectionManager httpConnectionManager;
	//private final static String URL1 = "http://10.244.168.198/personificat/";
	private final static String URL1 = "http://10.244.231.198/personificat/";
	static {
		httpConnectionManager = new MultiThreadedHttpConnectionManager();
		HttpConnectionManagerParams params = httpConnectionManager.getParams();
		params.setConnectionTimeout(CONNECTION_TIMEOUT);
		params.setSoTimeout(SOCKET_TIMEOUT);
		params.setDefaultMaxConnectionsPerHost(MAX_CONN_PRE_HOST);
		params.setMaxTotalConnections(MAX_CONN);
	}

	public static String doPost(String methodParm, String dataJson, String charset) {
		StringBuffer response = new StringBuffer();
		HttpClient client = new HttpClient(httpConnectionManager);
		PostMethod method = null;
		InputStream responseBodyStream = null;
		InputStreamReader streamReader = null;
		BufferedReader reader = null;
		try {
			method = new PostMethod(URL1 + methodParm);
			method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
			if (dataJson != null) {
				NameValuePair[] param = new NameValuePair[1];
				param[0] = new NameValuePair("data", dataJson);
				method.setRequestBody(param);
			}
			client.executeMethod(method);
			if (method.getStatusCode() == HttpStatus.SC_OK) {
				responseBodyStream = method.getResponseBodyAsStream();
				streamReader = new InputStreamReader(responseBodyStream, charset);
				reader = new BufferedReader(streamReader);
				String line;
				while ((line = reader.readLine()) != null) {
					response.append(line);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (responseBodyStream != null) {
					responseBodyStream.close();
				}
				if (streamReader != null) {
					streamReader.close();
				}
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			method.releaseConnection();
		}
		return response.toString();
	}

	public static String getJsonString(String userid,String password) {
		JSONObject json=new JSONObject();
		json.put("empno", userid);
		if(password!=null) {
			json.put("password", password);
		}
		json.put("httpKey", HTTP_KEY);
		return json.toString();
	}
	
	public static JSONObject getJsonData(String method,String jsonParam) {
		String jsonStr = doPost(method, jsonParam, "utf-8");
		JSONObject json=JSONObject.fromObject(jsonStr);
		return json;
	}
	
	public static void main(String[] args) {
		JSONObject json = new JSONObject();
		//json.put("depart", "TOS91705");
		json.put("empno", "C3006064");
		//json.put("password", "qiu.2020");
		json.put("httpKey", "B5618B7247257862E050F40A65E7F9C6");
		String method = "obtainToAccount";// 自己填写
		String jsonStr = doPost(method, json.toString(), "utf-8");
		
	
//		JSONObject json1=JSONObject.fromObject(jsonStr);
//		
//		JSONObject jsonparm = JSONObject.fromObject(json1.get("data"));
//		AccountInfo dto = (AccountInfo) JSONObject.toBean(jsonparm, AccountInfo.class);
		System.out.println(jsonStr);
	}

}
